﻿using KWT.DRG_DIP.API.Infrastructure;
using KWT.DRG_DIP.DB;
using KWT.DRG_DIP.DTO.Basic.Role;
using KWT.DRG_DIP.PO.Basic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace KWT.DRG_DIP.API.Controllers.Basic
{
    /// <summary>
    /// 角色
    /// </summary>
    [ApiExplorerSettings(GroupName = "basic")]
    public class RoleController : BasicControllerBase
    {
        public EFContext main { get; set; }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult List()
        {
            var list = main.Set<Sys_Role>().OrderByDescending(x => x.AddTime).Select(x => new { key = x.RoleID, label = x.Name }).ToList();
            return Output(list != null, "", "", list);
        }

        /// <summary>
        /// 根据部门ID查列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ListByDeptID([FromQuery] string deptID)
        {
            var list = (from x in ef.Set<Sys_Role>().AsQueryable()
                        join y in ef.Set<Dept_Role>().AsQueryable() on x.RoleID equals y.RoleID
                        where y.DeptID == deptID
                        select new { x.Name, x.RoleID }).AsNoTracking().ToList();

            return Output(list != null, "", "", list);
        }

        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddOrUpdate(InAddOrUpdate dto)
        {
            if (string.IsNullOrEmpty(dto.RoleID))
            {
                var role = new Sys_Role()
                {
                    RoleID = "RO" + snow_id.CreateId().ToString(),
                    Name = dto.Name
                };
                main.Add(role);
            }
            else
            {
                var role = main.Find<Sys_Role>(dto.RoleID);
                role.Name = dto.Name;
                main.Attach(role);
            }
            var flag = main.SaveChanges() > 0;
            return Output(flag, "操作成功！", "操作失败！");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Del(InDel dto)
        {
            var flag = main.Set<Sys_Role>().Where(x => dto.RoleIDs.Contains(x.RoleID)).ExecuteDelete() > 0;
            return Output(flag, "删除成功！", "删除失败！");
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Detail(string id)
        {
            var role = main.Find<Sys_Role>(id);
            return Output(role != null, "", "", role);
        }

        /// <summary>
        /// 角色列表（级联组件使用）
        /// </summary>
        /// <param name="deptID"></param>
        /// <returns></returns>
        [HttpGet]
        public OutputResult CascaderList([FromQuery] string deptID)
        {
            if (deptID.Equals("1"))
            {
                var q = (from x in ef.Set<Sys_Dept>().AsQueryable()
                         join y in ef.Set<Dept_Role>().AsQueryable() on x.DeptID equals y.DeptID
                         join z in ef.Set<Sys_Role>().AsQueryable() on y.RoleID equals z.RoleID
                         where x.DeptID == deptID
                         select new { label = z.Name, value = z.RoleID, children = new List<string>(), loading = false, type = "role" });
                var list = q.ToList();
                return Output(list.Count > 0, "", "", list);
            }
            else
            {
                var q = (from x in ef.Set<Sys_Dept>().AsQueryable()
                         join y in ef.Set<Dept_Role>().AsQueryable() on x.DeptID equals y.DeptID
                         join z in ef.Set<Sys_Role>().AsQueryable() on y.RoleID equals z.RoleID
                         where x.DeptID == deptID
                         select new { label = z.Name, value = z.RoleID, type = "role" });
                var list = q.ToList();
                return Output(list.Count > 0, "", "", list);
            }
        }
    }
}
